//初始数据
var str_length = null;
var zt = 0;
var row = null;
var neirong = null;
$('#yz-tlebel').focus();
exeShowPage();
function  exeShowPage(){
	var params = {
		"jinyongbz":zt
	}
	$.ajax({
		//请求方式为get
		type: "get",
		//获取json文件路径
		url: "./yizhudy/getCount",
		data: params,
		async: false,
		//请求成功完成后要执行的方法
		success: function(data){
			totalNumber = data;
		}
	});
    
    function createDemo(name) {
    	$('#tbyizhumb').html('');
		var container = $('#pagination-' + name);
		container.pagination({
		dataSource: './yizhudy/getByPage?jinyongbz=' + encodeURI(zt),
		locator: 'entities',
		pageSize: 12,
		totalNumber: totalNumber,
		 //增加总记录数
		showNavigator: true,
   	    formatNavigator: '共 <%= totalNumber %> 条',
		callback: function(data, pagination){
			var content = '';
			$(".Tb-list").html("");
            	  $.each(data, function(i, v){
  	             	    var perPageRecordNum = pagination.pageSize; //每页数据行数
 	            		var pageNo = pagination.pageNumber; //当前页号
 	            		var index = (((pageNo-1) * perPageRecordNum) + i + 1);
 	             	 	var content = "<tr id='" + index + "'>";
 	             		//序号控制
 	             	 	content = content +"<td width='8%'><label class='label'>" + (((pageNo - 1) * perPageRecordNum) + i + 1) + "</label></td>";
	 	                content = content +"<td width='55%'><input type='text' class='input-sm col-sm-12 u-input' id='yizhu' name='yizhu' /></td>";
	 	                content = content +"<td width='15%' style='overflow: visible;'>" +
											'<div class="menu-up choose dropdown-search-li none-border" style="position: relative;">' +
	                            			'<input type="hidden" id="yizhumb-zt" value="0">' +
	                            			'<div  class="btn dropdown-toggle select-menu" data-toggle="dropdown" tabindex="0" id="lzhuangtai"><span></span></div>' +
	                                		'<ul class="dropdown-menu" role="menu" id="lbzhuangtai"><li value="1">禁用</li><li value="0">启用</li></ul></div>' +
	 	                 					"</td>" ;
	 	                 content = content +'<td width="15%"><a class="operation-left"  id="yz-quedin" onclick="tianjia(' + data[i].id + ',' + (((pageNo - 1) * perPageRecordNum) + i + 1) + ')">保存</a>';
	 	                 content = content +'</td>';
	 	                 content = content +"</tr>";
	 	                $(".Tb-list").append(content);
	 	                 //内容读取
	 	                 $("#"+index).find("#yizhu").val(data[i].neirong);
	 	                 // 禁启数据读取
	 					$("#"+index).find("#yizhumb-zt").val(data[i].jinyongbz);
	 					var $span = $("<span></span>");
	 					//var $b = $("<b></b>");
	 					if(data[i].jinyongbz == 0){
	 						var text = '启用';
	 					}else{
	 						var text = '禁用';
	 					}
	 					$("#"+index).find("#lzhuangtai span").html(text);
 	             });
             }
         });
		return container;
       }
    createDemo('fenyyizhu');
	if(totalNumber <= 12) {
		$('#pagination-fenyyizhu').css('display','none');
	}else{
		$('#pagination-fenyyizhu').css('display','block');
	}
}
  
 //添加按钮
 $("#yz-add").click(function(){
	 //字数限制限制校验
	 if($("#yz-tlebel").val().length > 200){
		 $("#yz-tlebel").addClass("f-jiaoyan");
		 notifyWarning("可输入最大长度为200个字符");
		 yzflag=false;
		 return false;
	 }else if($("#yz-tlebel").val() == '') {
		 $("#yz-tlebel").addClass("f-jiaoyan");
		 notifyWarning("医嘱信息不能为空");
		 yzflag=false;
		 return false;
	 }else {
		 yzflag=true;
	 }
	 if(!yzflag)return;
	 var  add_neirong = $("#yz-tlebel").val();
	 var  add_zt = $("#yizhumb-zt").val();
	 var sendData = {
	    	   "neirong": add_neirong,
	           "jinyongbz": add_zt,
	      };
      $.ajax({
        //传递类型
        type:"POST",
        //传递地址
        url:'./yizhudy/xinjianyzdy',
        //传送数据
        data:sendData,
        success:function(data){
    	  if (data == MSG_CHONGFUBZ) {
			notifyWarning("该医嘱已在诊所中");
			return false;
    	  }
			//重置
			$("#yz-tlebel").val("");
			$("#yizhumb-zt").val();
			notifySuccess('保存成功');
			exeShowPage();
	        }
      });
      $('#yz-tlebel').focus();
 });
 
//重置加按钮的内容
$("#yz-treset").click(function(){
   	$("#yz-tlebel").val("");
   	$('#yizhumb-zt').val(0);
	$('#lzhuangtai').text("");
	var $span = $("<span></span>");
	$('#lzhuangtai').append($span);
	$('#lzhuangtai span').text("启用");
	$("#yz-tlebel").removeClass("f-jiaoyan");
});

//列表确定
var yzflag=true;
function tianjia(id,trid){
	if(!yzflag)return;
		if($("#"+trid).find("#yizhu").val() == ""){
			$(this).addClass("f-jiaoyan");
			notifyWarning("医嘱信息不能为空");
			return false;
		}
	$(".u-input").removeClass("td-border1");
    var tjzt = $("#"+trid).find("#yizhumb-zt").val();
    row = id;
    neirong = $("#"+trid).find("#yizhu").val();
    var sendData = {
		"id":row,
		"neirong": neirong,
		"jinyongbz": tjzt
	}
	$.ajax({
		//传递类型
		type:"POST",
		//传递地址
		url:'./yizhudy/xiugaiyzdy',
		//传送数据
		data:sendData,
		success:function(data){
			if (data == MSG_CHONGFUBZ) {
				notifyWarning("该附加费已在诊所中");
				return false;
    	  	}
			exeShowPage();
			notifySuccess('保存成功');
		}
	});
}

//搜索按钮 
$("#yz-search").click(function(){
	zt = $("#yizhumb-cxzt").val();
	//显示全部判断
	if(zt == 2){
		zt = '';
	}
	exeShowPage();
	return false;
});
 
//搜索条件重置
$("#yz-reset").click(function(){
	$('#yizhumb-cxzt').val(0);
	$('#zhuangtai').text("");
	var $span = $("<span></span>");
	$('#zhuangtai').append($span);
	$('#zhuangtai span').text("启用");
	//重置查询
	zt = $("#yizhumb-cxzt").val();
	//显示全部判断
	if(zt == 2){
		zt = '';
	}
	exeShowPage();
	return false;
});

//查询select模拟
$(".dropdown-menu li").click(function(){
	var bText = $(this).text();
	var bValue = $(this).val();
	$(this).parent().parent().find(".dropdown-toggle").text("");
	var $span = $("<span></span>");
	$(this).parent().parent().find('.dropdown-toggle').append($span);
	$(this).parent().parent().find('.dropdown-toggle span').text(bText);
	$(this).parent().parent().find("#yizhumb-cxzt").val(bValue);
	$(this).parent().css("display","none");
});

//列表select模拟
$("#cxzhuangtai li").click(function(){
	var bText = $(this).text();
	var bValue = $(this).val();
	$(this).parent().parent().find(".dropdown-toggle").text("");
	var $span = $("<span></span>");
	$(this).parent().parent().find('.dropdown-toggle').append($span);
	$(this).parent().parent().find('.dropdown-toggle span').text(bText);
	$(this).parent().parent().find("#yizhumb-zt").val(bValue);
	$(this).parent().css("display","none");
	//下拉框改变立即查询
	zt= $("#yizhumb-cxzt").val();
	//显示全部判断
	if(zt==2){
		zt='';
	}
	exeShowPage();
 });
/*加载下拉菜单*/
$(document).on('click','#tbzhuangtai li',function(){
	var bText = $(this).text();
	var bValue = $(this).val();
	$(this).parent().parent().find(".dropdown-toggle").text("");
	var $span = $("<span></span>");
	$(this).parent().parent().find('.dropdown-toggle').append($span);
	$(this).parent().parent().find('.dropdown-toggle span').text(bText);
	$(this).parent().parent().find("#yizhumb-zt").val(bValue);
	$(this).parent().css("display","none");
});

$(document).on("blur",".Tb-list .input-sm",function(){
	//非空校验
	if($(this).val() == ""){
		$(this).addClass("f-jiaoyan");
		notifyWarning("医嘱信息不能为空");
		return false;
	}else{	    		
	 	$(this).removeClass("f-jiaoyan");
	 	return false;
	}
});
$(document).on("input",".Tb-list .input-sm",function(){
	//字数校验
	if($(this).val().length>200){
		$(this).addClass("f-jiaoyan");
		notifyWarning("可输入最大长度为200个字符");
		return false;
	}else{
		$(this).removeClass("f-jiaoyan");
		return false;
	}
});
	 
//校验 
$("#yz-tlebel").focusout(function(){
	//非空校验
	if($(this).val() ==""){
		return false;
	}else{
		$("#yz-tlebel").removeClass("f-jiaoyan");
		return false;
	}
	//字数限制
	if($("#yz-tlebel").val().length>200){
		$("#yz-tlebel").addClass("f-jiaoyan");
		yzflag=false;
		return false;
	}else{
		$(this).removeClass("f-jiaoyan");
		yzflag=true;
		return false;
	}
	});  
$(document).on("focusout","table select",function(){
	if($(this).find("option:selected").val() == ""){
		return false;
	}else{
		$(this).removeClass("f-jiaoyan");
	}
});
	  
$("#yz-tlebel").on('input',function(){
	var tt = $(this).val();
	if(tt.length>200){
		yzflag=false;
		$(this).addClass("n-invalid");
		notifyWarning("可输入最大长度为200个字符");
	}else if(tt.length<200){
		yzflag=true;
		$(this).removeClass("n-invalid");
	}
});
$(document).on('input','#yizhu',function(){
	var tt = $(this).val();
	if(tt.length>200){
		yzflag=false;
		$(this).addClass("n-invalid");
		notifyWarning("可输入最大长度为200个字符");
	}else if(tt.length<200){
		yzflag=true;
		$(this).removeClass("n-invalid");
	}
})
// 检测该诊所是否已存在该医嘱模板
function compareYiZhumbBymc(mingcheng) {
	var isZhensuo = true;
	$.ajax({
		type: "get",
		data: {"mingcheng": mingcheng},
		url: path + "/yizhudy/compareYiZhudyBymc",
		sync: false,
		success: function (data) {
		isZhensuo = data;
		}
	});
	return isZhensuo;
}